# coding=utf-8
import cv2
cv2.face
import numpy as np
import os
#加载训练数据集文件
recogizer=cv2.face.LBPHFaceRecognizer_create()
recogizer.read('trainer/trainer.yml')
#准备识别的图片
img=cv2.imread('C:\\Users\\asus\\Pictures\\Camera Roll\\111111.pgm')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
face_detector = cv2.CascadeClassifier("C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml")
faces =face_detector.detectMultiScale(gray)
for x, y,w, h in faces:
    cv2.rectangle(img,(x,y),(x+w, y+h),(0,255,0),2)
    # 人脸识别
    id, confidence = recogizer.predict(gray[y:y + h, x: x + w])

    print('标签id:', id,'置信评分: ', confidence)

cv2.imshow(' result',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# #coding=utf-8
# import cv2
# import numpy as np
# import os
# #加载训练数据集文件
# recogizer=cv2.face.LBPHFaceRecognizer_create()
# recogizer.read('trainer/trainer.yml')
# #准备识别的图片

# def face22():
#     #加载训练数据集文件
#     recogizer=cv2.face.LBPHFaceRecognizer_create()
#     recogizer.read('trainer/trainer.yml')
#     #准备识别的图片
#     img=cv2.imread('C:\\Users\\asus\\Pictures\\Camera Roll\\1000.pgm')
#     gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#     face_detector = cv2.CascadeClassifier("C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml")
#     faces =face_detector.detectMultiScale(gray)
#     for x, y,w, h in faces:
#         cv2.rectangle(img,(x,y),(x+w, y+h),(0,255,0),2)
#         # 人脸识别
#         id, confidence = recogizer.predict(gray[y:y + h, x: x + w])
#         print('标签id:', id,'置信评分: ', confidence)
#     cv2.imshow(' result',img)
#     cv2.waitKey(0)
#     cv2.destroyAllWindows()
#
# camera =cv2.VideoCapture(0)
# while True:
#     ret,frame =camera.read()
#     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # 将图片转化成灰度
#     f = cv2.resize(gray, (200, 200))
#     cv2.imwrite('C:\\Users\\asus\\Pictures\\Camera Roll\\1000.pgm', f)
#
#     # face22()
#     # 加载训练数据集文件
#     recogizer = cv2.face.LBPHFaceRecognizer_create()
#     recogizer.read('trainer/trainer.yml')
#     # 准备识别的图片
#     img = cv2.imread('C:\\Users\\asus\\Pictures\\Camera Roll\\1000.pgm')
#     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#     face_detector = cv2.CascadeClassifier("C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml")
#     faces = face_detector.detectMultiScale(gray)
#     for x, y, w, h in faces:
#         cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
#         # 人脸识别
#         id, confidence = recogizer.predict(gray[y:y + h, x: x + w])
#         print('标签id:', id, '置信评分: ', confidence)
#         if confidence>80:
#             cv2.putText(img,'wcpeng',(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),2)
#         cv2.imshow('camera', img)
#     if cv2.waitKey(1000 // 1200) & 0xff == ord('q'):
#         break
#     cv2.destroyAllWindows()
